/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;
import Models.KhuVuc_Model;
import Models.Ban_Model;
import Models.NhatKyHT_Model;
import Beans.NhanVien;

/** 
 *
 * @author Venus
 */
public class Ban_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vKhuVuc = new Vector();
                vKhuVuc = kvm.LayDSKVTaoBan(langid);
                Ban_Model bm = new Ban_Model();
                Vector vBan = new Vector();
                vBan = bm.LayDSBan();
                int makv = KhuVuc_Model.LayKVTaoBan(langid);
                Vector v = new Vector();
                v = bm.Map(Integer.toString(makv));
                request.setAttribute("DSMapBan", v);
                request.setAttribute("item", item);
                request.setAttribute("makv", Integer.toString(makv) );
                request.setAttribute("DSKV", vKhuVuc);
                request.setAttribute("DSBan", vBan);                
                RequestDispatcher rd = request.getRequestDispatcher("admin/newtable.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("UpdateKVBan")) {
                String item = request.getParameter("item");
                String makv = request.getParameter("makv");
                String maban = request.getParameter("maban");
                Ban_Model.UpdateKVBan(makv, maban);
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.LayDSBan();
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "table");
                request.setAttribute("DSBan", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listtable.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Add")) {
                String makv = request.getParameter("makv");
                String ban = request.getParameter("ban");
                String ghe = request.getParameter("ghe");
                Ban_Model.LapBan(makv, ban, ghe);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "table");
            } else if (action.equals("ShowBan")) {
                String item = request.getParameter("item");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.LayDSBan();
                request.setAttribute("DSBan", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listtable.jsp");
                rd.forward(request, response);
            } else if (action.equals("EditSoGhe")) {
                String maban = request.getParameter("maban");
                String soghe = request.getParameter("soghe");
                Ban_Model.EditSoGhe(maban, soghe);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "numberseat");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.DetailTable(maban);
                request.setAttribute("DSBan", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailtable.jsp");
                rd.forward(request, response);
            } else if (action.equals("UpdateStatusBan")) {
                String maban = request.getParameter("maban");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                Ban_Model.UpdateStatusBan(maban, status);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statustable");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.LayDSBan();
                request.setAttribute("item", item);
                request.setAttribute("DSBan", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listtable.jsp");
                rd.forward(request, response);
            } else if (action.equals("SearchBan")) {
                String tenkv = request.getParameter("tenkv");
                String tenban = request.getParameter("tenban");
                String status = request.getParameter("status");
                String soghe = request.getParameter("soghe");
                String item = request.getParameter("item");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.SearchBan(tenkv, tenban, status, soghe, langid);
                request.setAttribute("status", status);
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("tenban", tenban);
                request.setAttribute("soghe", soghe);
                request.setAttribute("item", item);
                request.setAttribute("DSBan", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchtable.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTenKV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.SortTenKV(langid, order);
                request.setAttribute("DSBan", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssorttable.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTTKV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.SortTTKV(langid, order);
                request.setAttribute("DSBan", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssorttable.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTenKV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tenkv = request.getParameter("tenkv");
                String tenban = request.getParameter("tenban");
                String status = request.getParameter("status");
                String soghe = request.getParameter("soghe");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.SortSearchTenKV(langid, order, tenkv, tenban, status, soghe);
                request.setAttribute("DSBan", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("tenban", tenban);
                request.setAttribute("status", status);
                request.setAttribute("soghe", soghe);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchtable.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTTKV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tenkv = request.getParameter("tenkv");
                String tenban = request.getParameter("tenban");
                String status = request.getParameter("status");
                String soghe = request.getParameter("soghe");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.SortSearchTTKV(langid, order, tenkv, tenban, status, soghe);
                request.setAttribute("DSBan", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("tenban", tenban);
                request.setAttribute("status", status);
                request.setAttribute("soghe", soghe);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchtable.jsp");
                rd.forward(request, response);
            } else if (action.equals("Map")) {
                String makv = request.getParameter("makv");
                String item = request.getParameter("item");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.Map(makv);
                request.setAttribute("DSBan", v);
                request.setAttribute("item", item);
                request.setAttribute("makv", makv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/maptable.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("ShowMap")) {
                String item = request.getParameter("item");
                int makv = KhuVuc_Model.LayKVTaoBan(langid);
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.Map(Integer.toString(makv));
                request.setAttribute("DSBan", v);
                request.setAttribute("item", item);
                request.setAttribute("makv", Integer.toString(makv) );
                RequestDispatcher rd = request.getRequestDispatcher("admin/maptable.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("DetailTable")) {
                String maban = request.getParameter("maban");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.DetailTable(maban);
                request.setAttribute("DSBan", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailtable.jsp");
                rd.forward(request, response);
            } else if (action.equals("UpdateTTBan")) {
                String mattban = request.getParameter("mattban");
                String maban = request.getParameter("maban");
                Ban_Model.UpdateTTBan(mattban, maban);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statustable");
                Ban_Model bm = new Ban_Model();
                Vector v = new Vector();
                v = bm.DetailTable(maban);
                request.setAttribute("DSBan", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailtable.jsp");
                rd.forward(request, response);
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
